# 公共的工具文件
from flask import session, current_app, g
from functools import wraps

from info.models import User


def do_rank(index):
    '''根据index,返回定义的first,second,third'''

    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return 'third'
    else:
        return ''

# view_func == news_derail
def user_login_data(view_func):
    '''使用装饰器的形式获取用户登录信息'''
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id', None)
        user =None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        g.user = user

        return view_func(*args, **kwargs)


    return wrapper
